package com.rjwh.dingdong.client.activity.drawbook;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.widget.ListView;

import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.adapter.DrawBookDetailCommentAdapter;
import com.rjwh.dingdong.client.bean.localbean.DrawBookCommentData;
import com.rjwh.dingdong.client.mvp.presenter.impl.DrawBookDetailCommentListPresenter;
import com.rjwh.dingdong.client.mvp.view.DrawBookDetailCommentListView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author Kay
 * @ClassName DrawBookDetailCommentListActivity
 * @Description 绘本评论列表
 * @date 2016-8-10
 * @history 2016-8-10 author: description:
 */
public class DrawBookDetailCommentListActivity extends BaseActivity implements DrawBookDetailCommentListView {
    private static final int INPUT_REQUESTCODE = 123;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    @BindView(R.id.draw_book_detail_comment_list_layout_lv)
    ListView drawBookDetailCommentListLayoutLv;
    @BindView(R.id.draw_book_detail_comment_list_mPullRefreshView)
    SwipyRefreshLayout drawBookDetailCommentListMPullRefreshView;

    private DrawBookDetailCommentListPresenter presenter;
    private DrawBookDetailCommentAdapter commentAdapter;
    private int pageNumber = 0;
    private int bookid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.draw_book_detail_comment_list_layout);
        ButterKnife.bind(this);
        parseIntent();
        initView();
        presenter = new DrawBookDetailCommentListPresenter(this, this);
        drawBookDetailCommentListMPullRefreshView.doPullRefreshing();
    }

    public static void startActivity(Context context, int bookid) {
        Intent intent = new Intent(context, DrawBookDetailCommentListActivity.class);
        intent.putExtra("bookid", bookid);
        context.startActivity(intent);
    }

    private void parseIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            bookid = intent.getIntExtra("bookid", 0);
        }
    }

    private void initView() {
        initToolbarHelper();
        drawBookDetailCommentListMPullRefreshView.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh(SwipyRefreshLayoutDirection direction) {
                switch (direction) {
                    case TOP:
                        pageNumber = 0;
                        doGetPictureBookCommentList(true, SwipyRefreshLayoutDirection.TOP);
                        break;

                    case BOTTOM:
                        pageNumber++;
                        doGetPictureBookCommentList(false, SwipyRefreshLayoutDirection.BOTTOM);
                        break;

                    default:
                        break;
                }
            }
        });
        commentAdapter = new DrawBookDetailCommentAdapter(this, false);
        drawBookDetailCommentListLayoutLv.setAdapter(commentAdapter);
    }

    private void doGetPictureBookCommentList(boolean isRefresh, SwipyRefreshLayoutDirection direction) {
        presenter.doGetPictureBookCommentList(bookid, pageNumber, isRefresh, direction);
    }

    private void initToolbarHelper() {
        this.setSupportActionBar(this.toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle(R.string.all_comment_title);
        }
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

    @Override
    public void setDirection(SwipyRefreshLayoutDirection direction) {
        drawBookDetailCommentListMPullRefreshView.setDirection(direction);
    }

    @Override
    public void setRefreshing(boolean refreshing) {
        drawBookDetailCommentListMPullRefreshView.setRefreshing(refreshing);
    }

    @Override
    public void loadData(boolean isRefresh, List<DrawBookCommentData> drawbookcommentlist) {
        if (isRefresh) {
            commentAdapter.clearAll();
        }
        commentAdapter.appendToList(drawbookcommentlist);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }

    @OnClick(R.id.draw_book_detail_comment_list_layout_reply_root_ll)
    public void onClick() {
        Intent intent = new Intent(DrawBookDetailCommentListActivity.this, DrawBookDetailCommentInputActivity.class);
        intent.putExtra("bookid", bookid);
        intent.putExtra("tag", 1);
        startActivityForResult(intent, INPUT_REQUESTCODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == INPUT_REQUESTCODE) {
            if (resultCode == RESULT_OK) {
                //评论完刷新列表
                presenter.doGetPictureBookCommentList(bookid, 0, true, SwipyRefreshLayoutDirection.TOP);
            }
        }
    }
}
